﻿using System;
using System.ComponentModel.Composition.Hosting;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Cubicle.Core.Interfaces;

namespace Cubicle.Service.Hosting
{
    public class ComposedServiceHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            CompositionContainer container = ComposedServiceHosts.CompositionContainer;

            if (container == null)
                throw new InvalidOperationException();

            var engine = container.GetExportedValue<IEngine>();
            return new ServiceHost(engine, baseAddresses);
            //return CreateComposedServiceHost(serviceType, baseAddresses, container);
        }

        protected virtual ComposedServiceHost CreateComposedServiceHost(Type serviceType, Uri[] baseAddresses, CompositionContainer container)
        {
            return new ComposedServiceHost(serviceType, container, baseAddresses);
        }
    }
}